package aiyh.utils.sqlUtil.whereUtil.impl;

import aiyh.utils.sqlUtil.whereUtil.InnerWhere;
import aiyh.utils.sqlUtil.whereUtil.InnerWhereOperator;

import java.util.List;

/**
 * @author EBU7-dev1-ayh
 * @date 2021/8/23 0023 22:20
 */


public class InnerPrepWhereOperatorImpl implements InnerWhereOperator {
	private final InnerPrepWhereImpl innerPrepWhere;

	public InnerPrepWhereOperatorImpl(InnerPrepWhereImpl innerPrepWhere) {
		this.innerPrepWhere = innerPrepWhere;
	}

	@Override
	public InnerWhere whereNull() {
		this.innerPrepWhere.whereAppend(" is null ");
		return this.innerPrepWhere;
	}

	@Override
	public InnerWhere whereNotNull() {
		this.innerPrepWhere.whereAnd(" is not null ");
		return this.innerPrepWhere;
	}

	@Override
	public InnerWhere whereEqual(Object value) {
		this.innerPrepWhere.whereAppend(" = ? ");
		this.innerPrepWhere.addArgs(value);
		return this.innerPrepWhere;
	}

	@Override
	public InnerWhere whereNotEqual(Object value) {
		this.innerPrepWhere.whereAppend(" <> ? ");
		this.innerPrepWhere.addArgs(value);
		return this.innerPrepWhere;
	}

	@Override
	public InnerWhere greaterThan(Object value) {
		this.innerPrepWhere.whereAppend(" > ? ");
		this.innerPrepWhere.addArgs(value);
		return this.innerPrepWhere;
	}

	@Override
	public InnerWhere lessThan(Object value) {
		this.innerPrepWhere.whereAppend(" < ? ");
		this.innerPrepWhere.addArgs(value);
		return this.innerPrepWhere;
	}

	@Override
	public InnerWhere greaterOrEqual(Object value) {
		this.innerPrepWhere.whereAppend(" >= ? ");
		this.innerPrepWhere.addArgs(value);
		return this.innerPrepWhere;
	}

	@Override
	public InnerWhere lessThanOrEqual(Object value) {
		this.innerPrepWhere.whereAppend(" <= ? ");
		this.innerPrepWhere.addArgs(value);
		return this.innerPrepWhere;
	}

	@Override
	public InnerWhere BetweenAnd(Object start, Object end) {
		this.innerPrepWhere.whereAppend(" between ? ");
		this.innerPrepWhere.addArgs(start);
		this.innerPrepWhere.whereAppend(" and ? ");
		this.innerPrepWhere.addArgs(end);
		return this.innerPrepWhere;
	}

	@Override
	public InnerWhere whereIn(Object... values) {
		this.innerPrepWhere.whereAppend(" in ( ");
		for (int i = 0; i < values.length; i++) {
			this.innerPrepWhere.whereAppend(" ? ");
			this.innerPrepWhere.addArgs(values[i]);
			if (i < values.length - 1) {
				this.innerPrepWhere.whereAppend(",");
			}
		}
		this.innerPrepWhere.whereAppend(" ) ");
		return this.innerPrepWhere;
	}

	@Override
	public InnerWhere whereInList(List<Object> list) {
		this.innerPrepWhere.whereAppend(" in ( ");
		for (int i = 0; i < list.size(); i++) {
			this.innerPrepWhere.whereAppend(" ? ");
			this.innerPrepWhere.addArgs(list.get(i));
			if (i < list.size() - 1) {
				this.innerPrepWhere.whereAppend(",");
			}
		}
		this.innerPrepWhere.whereAppend(" ) ");
		return this.innerPrepWhere;
	}

	@Override
	public InnerWhere whereNotIn(Object... values) {
		this.innerPrepWhere.whereAppend(" not in ( ");
		for (int i = 0; i < values.length; i++) {
			this.innerPrepWhere.whereAppend(" ? ");
			this.innerPrepWhere.addArgs(values[i]);
			if (i < values.length - 1) {
				this.innerPrepWhere.whereAppend(",");
			}
		}
		this.innerPrepWhere.whereAppend(" ) ");
		return this.innerPrepWhere;
	}

	@Override
	public InnerWhere whereNotInList(List<Object> list) {
		this.innerPrepWhere.whereAppend(" not in ( ");
		for (int i = 0; i < list.size(); i++) {
			this.innerPrepWhere.whereAppend(" ? ");
			this.innerPrepWhere.addArgs(list.get(i));
			if (i < list.size() - 1) {
				this.innerPrepWhere.whereAppend(", ");
			}
		}
		this.innerPrepWhere.whereAppend(" ) ");
		return this.innerPrepWhere;
	}

	@Override
	public InnerWhere inSql(Object sql) {
		this.innerPrepWhere.whereAppend(" in ( ");
		this.innerPrepWhere.whereAppend(sql);
		this.innerPrepWhere.whereAppend(" ) ");
		return this.innerPrepWhere;
	}

	@Override
	public InnerWhere notInSql(Object sql) {
		this.innerPrepWhere.whereAppend(" not in ( ");
		this.innerPrepWhere.whereAppend(sql);
		this.innerPrepWhere.whereAppend(" ) ");
		return this.innerPrepWhere;
	}

	@Override
	public InnerWhere whereLike(Object value) {
		this.innerPrepWhere.whereAppend(" like ?");
		this.innerPrepWhere.addArgs(value);
		return this.innerPrepWhere;
	}

	@Override
	public InnerWhere whereNotLike(Object value) {
		this.innerPrepWhere.whereAppend(" not like ?");
		this.innerPrepWhere.addArgs(value);
		return this.innerPrepWhere;
	}

	@Override
	public InnerWhere whereExists(Object... values) {
		this.innerPrepWhere.whereAppend(" exists ( ");
		for (int i = 0; i < values.length; i++) {
			this.innerPrepWhere.whereAppend(" ? ");
			this.innerPrepWhere.addArgs(values[i]);
			if (i < values.length - 1) {
				this.innerPrepWhere.whereAppend(",");
			}
		}
		this.innerPrepWhere.whereAppend(" ) ");
		return this.innerPrepWhere;
	}

	@Override
	public InnerWhere whereNotExists(Object... values) {
		this.innerPrepWhere.whereAppend(" not exists ( ");
		for (int i = 0; i < values.length; i++) {
			this.innerPrepWhere.whereAppend(" ? ");
			this.innerPrepWhere.addArgs(values[i]);
			if (i < values.length - 1) {
				this.innerPrepWhere.whereAppend(",");
			}
		}
		this.innerPrepWhere.whereAppend(" ) ");
		return this.innerPrepWhere;
	}

	@Override
	public InnerWhere whereExistsList(List<Object> list) {
		this.innerPrepWhere.whereAppend(" exists ( ");
		for (int i = 0; i < list.size(); i++) {
			this.innerPrepWhere.whereAppend(" ? ");
			this.innerPrepWhere.addArgs(list.get(i));
			if (i < list.size() - 1) {
				this.innerPrepWhere.whereAppend(", ");
			}
		}
		this.innerPrepWhere.whereAppend(" ) ");
		return this.innerPrepWhere;
	}

	@Override
	public InnerWhere whereNotExistsList(List<Object> list) {
		this.innerPrepWhere.whereAppend(" not exists ( ");
		for (int i = 0; i < list.size(); i++) {
			this.innerPrepWhere.whereAppend(" ? ");
			this.innerPrepWhere.addArgs(list.get(i));
			if (i < list.size() - 1) {
				this.innerPrepWhere.whereAppend(", ");
			}
		}
		this.innerPrepWhere.whereAppend(" ) ");
		return this.innerPrepWhere;
	}

	@Override
	public InnerWhere whereExistsSql(Object sql) {
		this.innerPrepWhere.whereAppend(" exists ( ");
		this.innerPrepWhere.whereAppend(sql);
		this.innerPrepWhere.whereAppend(" ) ");
		return this.innerPrepWhere;
	}

	@Override
	public InnerWhere whereNotExistsSql(Object sql) {
		this.innerPrepWhere.whereAppend(" not exists ( ");
		this.innerPrepWhere.whereAppend(sql);
		this.innerPrepWhere.whereAppend(" ) ");
		return this.innerPrepWhere;
	}
}
